/*测试数据*/
var usertestdata=[
	{
		id: 1,
		userId: 2322,
		phone: 18817802281,
		realName: 'lisd',
		address: '上海市浦东新区',
		shoppingAddress: '上海市浦东新区',
		createTime: 1502937009,
		userMoney: 2366,
		taxType: '个人', //公司、个人
		taxCompany: '上海煜众网络科技有限公司',
		taxId: 'CX343344343',
		taxPhone: '18817802221',
		taxAddress: '上海工商银行宝山支行',
		taxBankId: '33222331453233434',
		taxBankName: '上海工商银行宝山支行'
	},
	{
		id: 2,
		userId: 2322,
		phone: 18817802281,
		realName: 'lisd',
		address: '上海市浦东新区',
		shoppingAddress: '上海市浦东新区',
		createTime: 1502937009,
		userMoney: 2366,
		taxType: '个人', //公司、个人
		taxCompany: '上海煜众网络科技有限公司',
		taxId: 'CX343344343',
		taxPhone: '18817802221',
		taxAddress: '上海工商银行宝山支行',
		taxBankId: '33222331453233434',
		taxBankName: '上海工商银行宝山支行'
	}
];
var protestdata=[
	{
		id: 1,
		productID: 2322,
		productName: "18817802281",
		productBrand: 'linda',
		productVersion: 'V3高规',
		store: 3455,
		singlePrice: 234,
		tradeCnt: 2234,
		createTime: 1502937009
	},
	{
		id: 2,
		productID: 334,
		productName: "18817802281",
		productBrand: 'linda',
		productVersion: 'V3高规',
		store: 3455,
		singlePrice: 234,
		tradeCnt: 2234,
		createTime: 1502937009
	}
];


var isKF=true;
$(function(){
    getSupplierData();
	var tgid=getUrlParam("tgid");
	if(tgid!=null && tgid!=""){
		getStoreDataMes();
	}

	$("#btnCreateOrder").on('click',function(){
		createOrder();
	});
});
/*获取供应商数据并填充*/
function getSupplierData(){
    $.post(
        gobalServer+"supplier/selectAll",
        {},
        function(backData){
            $("#brandSelect").empty();
            /*var option = $("<option>").attr({"value":0}).text("品牌查询");
            $("#brandSelect").append(option);*/
            if(backData.statusCode==200){
                $.each(backData.commonList,function (index,item) {
                    var option = $("<option>").attr({"value":item.supplierName}).text(item.supplierName);
                    $("#supplierSelect").append(option);
                })
            }else{
                consoleMes($("#manageContent"),2000,"warning","无供应商数据，你可以添加相关供应商！");//success、info、warning、danger

            }
        }
    );
};
/*选购产品展示*/
function getStoreDataMes() {
    var tgid=getUrlParam("tgid");
	if(tgid!=null && tgid!=""){
        $.post(
            gobalServer+"instock/selectOne",
            {"id":tgid},
            function(backData){
                if(backData.statusCode==200){
                    var datarow=backData.object;
                    if(datarow.productId!=null){
                        $("#manageContent").attr({"tgid":datarow.id,"proid":datarow.productId});
                        $("#productId").text(datarow.productId);
                    }
                    if(datarow.productName!=null){
                        $("#productName").text(datarow.productName);
                    }
                    if(datarow.productBrand!=null){
                        $("#productBrand").text(datarow.productBrand);
                    }
                    if(datarow.productVersion!=null){
                        $("#productVersion").text(datarow.productVersion);
                    }
                    if(datarow.presentNumber!=null){
                        $("#nowProductStore").text(datarow.presentNumber);
                    }
                    if(datarow.addNumber!=null){
                        $("#addProductStore").text(datarow.addNumber);
                        $("#storeAddNum").val(datarow.addNumber);
                    }
                    if(datarow.supplier!=null){
                        $("#supplierSelect").val(datarow.supplier);
                    }
                    if(datarow.inStockPrice!=null){
                        $("#storeAddPrice").val(datarow.inStockPrice);
                    }
                }else{
                    consoleMes($("#manageContent"),3000,"warning",backData.msg);//success、info、warning、danger
                }
            }
        );
	}


}

/*开单*/
function createOrder(){
    var tgid =  $("#manageContent").attr("tgid");
    var productId =  $("#manageContent").attr("proid");
	var storeAddNum=$("#storeAddNum").val().trim();
    var supplierSelect=$("#supplierSelect").val().trim();
    var storeAddPrice=$("#storeAddPrice").val().trim();
    if(tgid==null || tgid==""){
        consoleMes($("#manageContent"),2000,"warning","该库存信息无法修改！");
        setTimeout(function () {
            window.location.href=gobalWeb+"product/storeshow.html";
        }, 2500);
        return;
    }
	if(storeAddNum==null || storeAddNum=="" || storeAddNum<=0){
		consoleMes($("#manageContent"),2000,"warning","请输入入库数量！");
		return;
	}
	if(supplierSelect==null || supplierSelect==""){
		consoleMes($("#manageContent"),2000,"warning","请选择供应商！");
		return;
	}
    if(storeAddPrice==null || storeAddPrice=="" || storeAddPrice<=0){
        consoleMes($("#manageContent"),2000,"warning","请输入入库价格！");
        return;
    }
	/*consoleMes($("#manageContent"),2000,"success","入库成功！");//success、info、warning、danger
	setTimeout(function () {
        window.location.href=gobalWeb+"product/storeshow.html?tgid="+productId;
    }, 2500);*/
    $.post(
        gobalServer+"instock/update",
        {"id":tgid,"addNumber":storeAddNum,"inStockPrice":storeAddPrice,"supplier":supplierSelect},
        function(backData){
            if(backData.statusCode==200){
                consoleMes($("#manageContent"),2000,"success","库存信息修改成功！");//success、info、warning、danger
                setTimeout(function () {
                    window.location.href=gobalWeb+"product/storeshow.html?tgid="+productId;
                }, 2500);
            }else{
                consoleMes($("#manageContent"),3000,"warning",backData.msg);//success、info、warning、danger
            }
        }
    );

		
};
